package com.dzy.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dzy.entity.Permission;
import com.dzy.service.PermissionService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    @GetMapping
    public String index(Model model) {
        List<Permission> list = this.permissionService.findAllMenu();
        model.addAttribute("list", list);
        return "permission/index";
    }

    @RequestMapping("/create")
    public String create(Model model,Permission permission){
        model.addAttribute("permission",permission);
        return "permission/create";
    }
    @RequestMapping("/save")
    public String save(Permission permission){
        this.permissionService.insert(permission);
        return "common/successPage";
    }
    /**
     * 编辑
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/edit/{id}")
    public String edit(Model model,@PathVariable Long id) {
        Permission permission = permissionService.getById(id);
        model.addAttribute("permission",permission);
        return "permission/edit";
    }

    /**
     * 保存更新
     */
    @PostMapping(value="/update")
    public String update(Permission permission) {
        permissionService.update(permission);
        return "common/successPage";
    }

    /**
     * 删除
     * @param
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        permissionService.delete(id);
        return "redirect:/permission";
    }
}
